package com.hireceivecard.contorller;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import com.hireceivecard.config.AppException;
import com.hireceivecard.config.IPUtil;
import com.hireceivecard.config.RedisOperator;
import com.hireceivecard.dto.ResetPasswordDto;
import com.hireceivecard.dto.UserLoginDto;
import com.hireceivecard.dto.UserRegisterDto;
import com.hireceivecard.pojo.Result;
import com.hireceivecard.service.CaptchaService;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(tags = "登陆注册")
public class LoginContorller {
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private RedisOperator redisOperator;

    /**
     * 登录接口
     * @param userLoginDto 用户实体
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result Login(@ApiParam(value = "用户登录实体", required = true) @RequestBody UserLoginDto loginDto) {

        return Result.success(loginDto.toString());
    }

    /**
     * 注册接口
     * @param userRegisterDto 用户实体
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result Register(@ApiParam(value = "用户注册实体", required = true) @RequestBody UserRegisterDto registerDto) {
        return Result.success(registerDto.toString());
    }


    @ApiOperation("重置密码")
    @PutMapping("/resetPassword")
    public Result ResetPassword(@ApiParam(value = "重置密码实体", required = true) @RequestBody ResetPasswordDto resetPasswordDto) {
        return Result.success("");
    }
    /**
     * 获取验证码接口
     * @param phone   用户手机
     * @param request
     * @return
     */
    @ApiOperation("获取验证码")
    @GetMapping("/getcaptcha")
    public Result send(@ApiParam(value = "手机号", required = true) @RequestParam("phone") String phone, HttpServletRequest request) {
        boolean isValid = PhoneUtil.isMobile(phone);
        if (!isValid) {
            AppException.getAppException("手机号格式不正确");
        }
        // 获得用户ip
        String userIp = IPUtil.getRequestIp(request);
        boolean ipExist = redisOperator.keyIsExist("MOBILE_SMSCODE" + ":" + userIp);
        if (ipExist) {
            AppException.getAppException("短信发送频率太高了~~！！！");
        }
        // 限制用户只能在60s以内获得一次验证码
        redisOperator.setnx60s("MOBILE_SMSCODE" + ":" + userIp, phone);
        return captchaService.getaptcha(phone, String.valueOf(RandomUtil.randomInt(1000, 10000)));
    }

}
